//
// Created by zlf on 20-1-12.
//

#ifndef TEST_ME_ODB_NOTETREE_H
#define TEST_ME_ODB_NOTETREE_H
#include <QtCore/QString>
#include <QtCore/QDateTime>
#include <QtCore/QDebug>

#include <vector>

#include <odb/core.hxx>
#include <odb/qt/lazy-ptr.hxx>
#include <odb/tr1/memory.hxx>

#include <odb/tr1/lazy-ptr.hxx>

#include "Note.hxx"

typedef std::vector<lazy_weak_ptr<Note>> Notes;


#pragma db object table("note_tree")

class NoteTree {
public:
	NoteTree() { parent_id_ = 0, type_ = 1, explain_ = ""; };

	NoteTree(QString name)
			: parent_id_(0), name_(name) {
		type_ = 1, explain_ = "";
		create_dt_ = QDateTime::currentDateTime();
		update_dt_ = QDateTime::currentDateTime();
	}

	NoteTree(int parent_id, QString name, QString explain = "")
			: parent_id_(parent_id), name_(name), explain_(explain) {
		id_ = 0;
		create_dt_ = QDateTime::currentDateTime();
		update_dt_ = QDateTime::currentDateTime();
	}

	~NoteTree() {
		qDebug() << "释放-：" << id_ << endl;
	}

	int id() const { return id_; }

	int parent_id() const { return parent_id_; };

	typedef ::Notes Notes_Type;

	const Notes_Type &notes() const { return notes_; }

	Notes_Type &notes() { return notes_; }

	const QString &name() const {
		return name_;
	}

	void setName(QString name) { name_ = name; }

	int type() { return type_; }

	const QDateTime &create_dt() { return create_dt_; }

	const QDateTime &update_dt() { return update_dt_; }

	const QString &explain() { return explain_; }

private:
	friend class odb::access;

#pragma db id auto type("INTEGER")
	int id_;
	int parent_id_;


#pragma db value_not_null inverse(notetree_)
	Notes_Type notes_;

#pragma db not_null type("VARCHAR(30)") column("name")
	QString name_;

#pragma db default(0)
	int type_;

	//在这里设置默认值无意义，自定义sql插入数据时有用
#pragma db not_null readonly options("DEFAULT CURRENT_TIMESTAMP")//readonly更新数据库时，对只读数据成员的更改将被忽略
	QDateTime create_dt_;

#pragma db not_null options("DEFAULT CURRENT_TIMESTAMP")
	QDateTime update_dt_;

	QString explain_;

};

#endif //TEST_ME_ODB_NOTETREE_H
